package com.liuyucheng.swing.timer;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

/**
 * @author LiuYuCheng
 * @date 2021年12月27日 10:44
 */
public class ReadProperties {

    private static final String filePath = System.getProperty("user.home")+File.separator+"record.properties";

    public static TimeBean readInfo() {
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(inputStream!=null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        TimeBean bean = new TimeBean();
        bean.setLastTime(properties.getProperty("lastTime"));
        bean.setTotalSeconds(Long.parseLong(properties.getProperty("totalSeconds")));
        return bean;
    }

    public static void setProperties(String value){
        Properties properties = new Properties();
        properties.setProperty("totalSeconds",value);
        properties.setProperty("lastTime",Timer.sdfSec.format(new Date()));
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(filePath);
            properties.store(outputStream,null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(outputStream!=null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(Timer.secondToTime(3601));
    }
}